
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var wxAuthFail = true;
var inviteuid = 0;
var hasMore = true;
var loadingMore = false;
var page = 1;
Page({
  data: {
    util: util,
    tc: 0,

    isIphone_x: false,
    nav_bgColor: '#ffffff',

    token: '',
    uid: 0,
    membercard: '',
    mingxiData: [],
    coin: 0
  },
  onLoad: function (data) {

    var that = this;
    var tc = app.get_tc();//简体繁体转换
    console.log("tc======", tc);
    that.setData({
      tc: tc,
    });
    inviteuid = data.inviteuid ? data.inviteuid : '';
    this.getLoginInfo();

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });


    wx.getSystemInfo({
      success: function (res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screen_h: res.windowHeight,
        });
      }
    });
    var titleName = tc == 1 ? '充值記錄' : '充值记录';

    wx.setNavigationBarTitle({
      title: titleName,
    })

    //获取关于等信息
    app.getAllAbout('contact', function (res) {
      that.setData({
        contact_phone: res
      })
    });
  },
  onShow: function () {
    var membercard = wx.getStorageSync('membercard');
    this.setData({
      membercard: membercard
    })
    console.log("wx.getStorageSync('membercard')", membercard)
  },



  loadMore: function (e) {

    if (hasMore && !loadingMore) {
      loadingMore = true;
      page++;
      console.log("page", page);
      this.loadMingxi(page);
    } else {
      console.log("没有更多了");
    }

  },



  //获取充值记录
  loadMingxi: function (cpage) {
    var that = this;
    util.api({
      method: "POST",
      data: {
        ac: "changelist",
        uid: that.data.uid,
        page: cpage,
        num: 20,
        coinonly: 1,
        "privatekey": utilMd5.hexMD5(that.data.uid + 'changelist' + that.data.token)
      },
      success: function (responseData) {
        var result = responseData.data;
        console.log("mingxi result ", result);
        if (result && result.length > 0) {
          for (var i = 0; i < result.length; i++) {
            var date = new Date(result[i].ctime * 1000);
            result[i].formatTime = util.formatTime(date, '-', ':');
            var coinnum = result[i].coinnum;
            result[i].formatCoinnum = (coinnum / 100.0).toFixed(2);
            result[i].coin_now_float = (result[i].coin_now / 100.0).toFixed(2);
            result[i].coinnum_float = (result[i].coinnum / 100.0).toFixed(2);
          }
          that.setData({
            mingxiData: that.data.mingxiData.concat(result),
          });
        } else {
          hasMore = false;
        }
      }
    });
  },

  getLoginInfo: function () {
    var that = this;
    app.getLoginUserInfo(function (res) {
      console.log("mine login:", res);
      wxAuthFail = res == -1;//用户拒绝授权
      if (wxAuthFail) {
        return;
      }
      
      that.setData({
        uid: res.uid,
        token: res.token,
        userInfo: res
      });
      that.loadMingxi(1);

      var params = {
        "uid": res.uid,
        "publickey": util.publiccode,
        "ac": "coininfo",
        "privatekey": utilMd5.hexMD5(res.uid + 'coininfo' + res.token),
        "tc": that.data.tc
      };
      console.log("余额参数：uid=" + res.uid + "; token = " + res.token, params);
      util.api({
        method: "post",
        data: params,
        success: function (responseData) {
          console.log("余额返回：", responseData);
          that.setData({
            coin: (responseData.data.coin || 0) / 100
          });
        }
      });

    });
  },


  goAtherPage: function (e) {
    console.log("goAtherPage", e)
    if (wxAuthFail) {
      app.goAuthorization();
    } else {
      var url = e.currentTarget.dataset.link;
      wx.navigateTo({ url: url })
    }

  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({ url: url })
  },



  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);
    var path = e.detail.value.nav_path;
    if (path && path.indexOf('navto:') == 0) {
      path = path.slice(6);
    }
    if (path) {
      wx.reLaunch({
        url: path,
      })
    }
  },



  //页面分享
  onShareAppMessage: function () {
    var invUid = this.data.uid ? this.data.uid : '';

    var path = '/pages/mine/mine?inviteuid=' + invUid;
    console.log("分享给好友：" + path);
    if (util.orgid == "21398") {
      return {
        title: this.data.tc == 1 ? '尚品高質凍肉海產零售批發小程序開通啦，歡迎下單購買！！！' : '尚品高质冻肉海产零售批发小程序开通啦，欢迎下单购买！！！',
        path: path
      };
    } else {
      return {
        title: '',
        path: path
      };
    }

  },


  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    });
    this.setData({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },





})